/**
 * Created by asus on 2016/9/2.
 */
//定义一个服务器的模块
var service=angular.module("service",[]);

//在服务器模块下定义一个控制器service_c1,用来控制电影内容部分的具体显示内容
service.controller("service_c1",["$scope","f1","$stateParams",function($scope,f1,$stateParams){
    $scope.count=5;
    $scope.page=1;
    $scope.start=0;

    $scope.get=function(){
        $scope.loading=true;
        f1.getData({count:$scope.count,start:$scope.start},$stateParams.p1,function(data){
            $scope.loading=false;
            $scope.totalPage=Math.ceil(data.total/$scope.count);
            $scope.data=data;
            $scope.$apply();
        });
    };
    $scope.previous=function(){
        $scope.page+=-1;
        if($scope.page<=0){
            $scope.page=1;
            return;
        }
        $scope.start+=-$scope.count;
        $scope.get();
    };
    $scope.next=function(){
        $scope.page+=1;
        if($scope.page>=$scope.totalPage+1){
            $scope.page=$scope.totalPage;
            return;
        }
        $scope.start+=$scope.count;
        $scope.get();
    };

    var getIfo=function(search){
        $scope.loading=true;
        f1.searchData({q:search,count:$scope.count,start:$scope.start},function(data){
            $scope.loading=false;
            $scope.totalPage=Math.ceil(data.total/$scope.count);
            $scope.data=data;
            $scope.$apply();
        });
    };
    //将search方法广播上去
    $scope.$emit("comeOn",getIfo);
    //将p1方法广播上去
    $scope.$emit("comeOn2",$stateParams.p1);
    //默认显示一部分
    $scope.get();
}]);

//自定义一个服务用来获取后台的数据
service.factory("f1",[function(){
    return {
        //从后台获取数据方法
        getData:function(objParams,type,fn){
            crossDomain("http://api.douban.com/v2/movie/"+type,objParams,function(data){
                fn(data);
            })
        },
        //search获取数据方法
        searchData:function(objParams,fn){
            crossDomain("http://api.douban.com/v2/movie/search",objParams,function(data){
                fn(data);
            })
        }
    }
}]);


